/*
 * KIMWONSORK@GMAIL.COM
 * COPYRIGHT CRAYONSOFT All rights reserved.
 * 2013오후 7:26:16
 * kws
 */

package com.crayon.account.dao;

import com.crayon.account.data.EDAType;
import com.crayon.util.GDB;
import com.crayon.util.UTILDate;
import com.crayon.util.UTILObj;
import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;

import net.fortuna.ical4j.model.DateList;
import net.fortuna.ical4j.model.ParameterList;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.model.PropertyList;
import net.fortuna.ical4j.model.parameter.Value;
import net.fortuna.ical4j.model.property.ExDate;

import java.util.Date;
import java.util.UUID;



public class DAOAccount extends DAORoot{

	DAOAccount(){
        super();
	}
	public DAOAccount(EDAType type){
		this();
		Date date = new Date(System.currentTimeMillis());
		setDtStart(date);
		setDtEnd(date);
		setxIOType(type.getKey());
	}

	@DatabaseField(unique=true,canBeNull=true)
	Date dtSyncKey;//SMS로 들어왔을 경우 유닉한 키값으로 처리한다. 
	
	@DatabaseField(canBeNull=false)
	Date dtStart;//시작일.
	@DatabaseField(canBeNull=false)
	Date dtEnd;//시작일.
	@DatabaseField(canBeNull=false)
	long xprice; //가격.
	@DatabaseField(canBeNull=false)
	int xEventType; //가계부/고정지출/예산/

	@DatabaseField
	Date dtUntil;//반복종료일.
	@DatabaseField
	String recur;//


	@DatabaseField
	String desciption;//메모 
	@DatabaseField
	int xdivofmonth; //할부나누기..


	@DatabaseField
	int xIOType; //수입/지출.

	@DatabaseField
	String xCategory; //사용처 카테고리...

	@DatabaseField
	String xaccountName; //은행 카드명..

	@DatabaseField
	String xUsePlace;//사용처 및 업체명..

	@DatabaseField
	String xImageKey;//

	@DatabaseField(dataType = DataType.BYTE_ARRAY)
	byte[] xImage;//제외 데이들.

	@DatabaseField(dataType = DataType.BYTE_ARRAY)
	byte[] exDates;//제외 데이들.

	@DatabaseField(dataType = DataType.BYTE_ARRAY)
	byte[] vEvents;//vevent type




	public Date getDtStart() {
		return dtStart;
	}



	public void setDtStart(Date dtStart) {
		this.dtStart = dtStart;
	}



	public Date getDtEnd() {
		return dtEnd;
	}



	public void setDtEnd(Date dtEnd) {
		this.dtEnd = dtEnd;
	}



	public Date getDtUntil() {
		return dtUntil;
	}



	public void setDtUntil(Date dtUntil) {
		this.dtUntil = dtUntil;
	}

	public long getXprice() {
		return xprice;
	}



	public void setXprice(long xprice) {
		this.xprice = xprice;
	}



	public String getDesciption() {
		return desciption;
	}



	public void setDesciption(String desciption) {
		this.desciption = desciption;
	}



	public int getXdivofmonth() {
		return xdivofmonth;
	}



	public void setXdivofmonth(int xdivofmonth) {
		this.xdivofmonth = xdivofmonth;
	}



	public int getxEventType() {
		return xEventType;
	}



	public void setxEventType(int xEventType) {
		this.xEventType = xEventType;
	}



	public String getxCategory() {
		return xCategory;
	}



	public void setxCategory(String xCategory) {
		this.xCategory = xCategory;
	}



	public String getXaccountName() {
		return xaccountName;
	}



	public void setXaccountName(String xaccountName) {
		this.xaccountName = xaccountName;
	}



	public String getxUsePlace() {
		return xUsePlace;
	}



	public void setxUsePlace(String xUsePlace) {
		this.xUsePlace = xUsePlace;
	}



	public byte[] getvEvents() {
		return vEvents;
	}



	public void setvEvents(byte[] vEvents) {
		this.vEvents = vEvents;
	}



	public int getxIOType() {
		return xIOType;
	}

	public void setxIOType(int xIOType) {
		this.xIOType = xIOType;
	}
	public String getRecur() {
		return recur;
	}
	public void setRecur(String recur) {
		this.recur = recur;
	}

	public DAOAccount createCron(){
		DAOAccount ical =null;
		try {
			ical =(DAOAccount)super.clone();
			if(ical!=null){
				return ical;
			}
		} catch (Exception e) {
			// TODO: handle exception

		}
		return ical;
	}
	public byte[] getExDates() {
		return exDates;
	}
	public void setExDates(byte[] exDates) {
		this.exDates = exDates;
	}
	
	public void addSaveExDate(Date pDate){
		ParameterList parameterList = new ParameterList();
		parameterList.add(Value.DATE);

		PropertyList list=null;
		if(getExDates()!=null){
			try {
				list = (PropertyList)UTILObj.convertByteArrayToObject(getExDates());
			} catch (Exception e) {
				// TODO: handle exception
				GDB.log(getClass(), " ERR getList " + e.getMessage());
			}
		}
		list = list!=null ? list : new PropertyList();
		try {
			GDB.log(getClass(), "예외 날짜 세팅 " + UTILDate.getYYYYMMDD(pDate));
			ExDate exdate =new ExDate(parameterList, UTILDate.getYYYYMMDD(pDate));
			list.add(exdate);

			GDB.log(getClass(), "예외 날짜 세팅 " + list.toString());

			byte[] bytearray = UTILObj.composeObjToByteArray(list);
			setExDates(bytearray);

		} catch (Exception e) {
			// TODO: handle exception
			GDB.log(getClass(), " ERR create date ex :::  " + e.getMessage());

		}
	}
	
	public PropertyList getExDateList(){
		if(getExDates()!=null ){
			try {
				PropertyList exList =(PropertyList)UTILObj.convertByteArrayToObject(getExDates());
				if(exList!=null){
					ParameterList parameterList = new ParameterList();
					parameterList.add(Value.DATE);
					PropertyList list =new PropertyList();
					for(int i = 0;i <exList.size();i++){
						ExDate date = (ExDate)exList.getProperties(Property.EXDATE).get(i);
						DateList dateList = date.getDates();
						for(int j=0;j < dateList.size();j++){
							Date exdate =(net.fortuna.ical4j.model.Date)dateList.get(j);
							GDB.log(getClass(), " EXDate list  valu e  "+ exdate);
//							exdate.setTime(exdate.getTime()-UTILDate.TIME_DAY);
							list.add(new ExDate(parameterList, exdate.toString()));
						}
					}
					return list;
				}				
			} catch (Exception e) {
				// TODO: handle exception
				GDB.log(getClass(), " EXDate ERR " + e.getMessage());
			}

		}
		return null;
	}
	public String getxImageKey() {
		return xImageKey;
	}
	public void setxImageKey(String xImageKey) {
		this.xImageKey = xImageKey;
	}
	public byte[] getxImage() {
		return xImage;
	}
	public void setxImage(byte[] xImage) {
		this.xImage = xImage;
	}
	public Date getDtSyncKey() {
		return dtSyncKey;
	}
	public void setDtSyncKey(Date dtSyncKey) {
		this.dtSyncKey = dtSyncKey;
	}
	
	
}
